package ru.urfc.portal.ui.client.googlets.client;

import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.controllers.GridController;
import ru.urfc.portal.ui.client.controllers.events.GetEvent;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;
import ru.urfc.portal.ui.client.descriptor.GridField;
import ru.urfc.portal.ui.client.model.catalogs.InvoiceState;
import ru.urfc.portal.ui.client.widgets.InvoiceFilter;
import ru.urfc.portal.ui.client.widgets.MegaGrid;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;

public class InvoiceList extends BaseGooglet {

	interface Binder extends UiBinder<Widget, InvoiceList> {}
	private static Binder binder = GWT.create(Binder.class);

	@UiField Element title;
	@UiField InvoiceFilter invoiceFilter;
	@UiField MegaGrid grid;
	@UiField Button search;

	public Type<GridController> SEARCH = new Type<GridController>();

	@UiHandler("search")
	void search(ClickEvent e) {
		getEventsDispatcher().fireEvent(new GetEvent(SEARCH));
	}
	
	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
		title.setInnerText("Счета");
		invoiceFilter.setupButton(search.getElement());

		GridDescriptor descriptor = new GridDescriptor(INVOICE, CLIENTS, getClientId(), INVOICES);
//		descriptor.addAnchorColumn("number", "id", PAGE_INVOICE_DETAILS, "Номер счета", 100);
		descriptor.addTextColumn("number", "Номер", 100);
		GridField autoExpandColumnField = descriptor.addTextColumn("name", "Предмет счета", 200);
		descriptor.addDateColumn("date", "Дата", 100);
		descriptor.addMoneyColumn("amountTotal", "Сумма", 100);
		descriptor.addBoolCIColumn("isPaid", InvoiceState.class, "Статус", 100);
		
		register(SEARCH, createController(descriptor, grid)).setFilter(invoiceFilter);
		grid.setAutoExpandColumn(autoExpandColumnField.getName());
	}
	
}